#include "utility.h"
#include <string.h>
#include "stm32f0xx_hal.h"

extern char uart_output[256];
extern DMA_HandleTypeDef hdma_usart1_tx;
extern UART_HandleTypeDef huart1;

void delay_us(uint32_t value)
{
  volatile uint32_t tmp = value;
  while (tmp--);
}

void UartSend(uint32_t length)
{
  uart_output[255] = 0;
  //设置uart发送DMA通道
  /* Disable the DMA */
  hdma_usart1_tx.Instance->CCR &= ~DMA_CCR_EN;
  /* Clear all flags */
  hdma_usart1_tx.DmaBaseAddress->IFCR = (DMA_FLAG_GL1 << hdma_usart1_tx.ChannelIndex);
  /* Configure DMA Channel data length */
  hdma_usart1_tx.Instance->CNDTR = length;
  /* Configure DMA Channel source address */
  hdma_usart1_tx.Instance->CPAR = (uint32_t)&huart1.Instance->TDR;
  /* Configure DMA Channel destination address */
  hdma_usart1_tx.Instance->CMAR = (uint32_t)uart_output;
  /* Enable the Peripheral */
  hdma_usart1_tx.Instance->CCR |= DMA_CCR_EN;
  /* Clear the TC flag in the ICR register */
  __HAL_UART_CLEAR_FLAG(&huart1, UART_CLEAR_TCF);
  ATOMIC_SET_BIT(huart1.Instance->CR3, USART_CR3_DMAT);
}
